import { App } from "../../../../src/App";
import { GameModule } from "../Game/GameModule";

const POS = [-10,10];
const { menu, ccclass, property } = cc._decorator;
@ccclass
@menu("斗地主/窗体组件/排行榜")
export default class RankView extends cc.Component {

    @property(cc.Node)
    item: cc.Node = null;
    @property(cc.Node)
    itemParent: cc.Node = null;
    @property(cc.Node)
    listNode: cc.Node = null;
    @property(cc.Node)
    listMenu: cc.Node = null;
    @property(cc.Node)
    comboBox: cc.Node = null;
    @property(cc.Node)
    comboBoxItem: cc.Node = null;
    @property(cc.Label)
    lblText: cc.Label = null;
    
    private _isAll:boolean = true;// true全部 false好友
    private _curIndex:number = 2;
    onLoad(){
        App.Event.on('GS2C_HallPersonalCenterChangeNameProto2', (data) => {

        }, this);

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.listNode.active = false;
    }

    start(){
        this.itemParent.removeAllChildren();
        GameModule.rankInfo.forEach((it,idx)=>{
            let item = cc.instantiate(this.item);
            item.parent = this.itemParent;
            App.Avatar.setHead(it.Avatar,App.Util.find('imgHead',item,'cc.Sprite'));
            App.setLableText(item,'lblName',it.UserName);
            App.setLableText(item,'lblCoin',App.Util.formatScore(it.Coin));
            App.setLableText(item,'lblFlag',GameModule.userTitle(it.Coin));
            App.setLableText(item,'lblRank',`第${idx+1}名`);
            it.OpenID ===  GameModule.userInfo.OpenID && (item.color = cc.color(232,194,156));
        });
    }

    onDownClick(){
        this.listNode.active = !this.listNode.active;
        this.listMenu.children.forEach((it:cc.Node,idx:number)=>{
            it.getChildByName('imgFlag').active = idx === this._curIndex;
        });
    }

    onChangeClick(){
        this._isAll = !this._isAll;
    }

    onSelectClick(_,data){
        // 0今日 1本周 2全部
        let id = parseInt(data);
        let i = 0;
        for (let it of this.listMenu.children){
            it.getChildByName('imgFlag').active = id === i;
            if (id === i){
                this.lblText.string = it.getComponent(cc.Label).string;
                this._curIndex = i;
            }
            ++i;
        }
        this.listNode.active = false;
    }

    onDestroy(){
        this.node.off(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
    }

    private onTouchStart(sender: cc.Event.EventTouch){
        //this.listNode.active = false;
    }
}